package com.wing.product.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.response.CelllnventoryAlarmExportVO;
import com.wing.cabinet.utils.ExcelUtils;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.service.ProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@Api(value = "Admin-采购预警信息", tags = "#/productCab/safetyInventory")
@RestController("safetyInventoryController")
@RequestMapping("cabProduct/admin/safetyInventory")
@RequiredArgsConstructor
@Slf4j
public class SafetyInventoryController {

    @Autowired
    private final ProductDetailService productDetailService;

    @ApiOperation(value = "查询采购预警信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuCode", value = "商品SKU编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "商品SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productName", value = "商品名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productBrandName", value = "商品品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String skuCode
            ,String skuName
            ,String productName
            ,String productBrandName
            ,String oldSkuCode
            ,String oldSpecCode
    ){
        Page<ProductDetail> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        paramMap.put("skuCode",skuCode);
        paramMap.put("skuName",skuName);
        paramMap.put("productName",productName);
        paramMap.put("productBrandName",productBrandName);
        paramMap.put("oldSkuCode",oldSkuCode);
        paramMap.put("oldSpecCode",oldSpecCode);
        IPage<ProductDetailVO> list =productDetailService.findWarningOfProcurement(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "导出按货道库存数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response, String enterpriseCode){
        List<ProductDetailVO> productDetailList = new ArrayList<>();
        Map paramsMap = new HashMap();
        paramsMap.put("enterpriseCode",enterpriseCode);
        Page<ProductDetail> pg = new Page<>(1, 99999);
        IPage<ProductDetailVO> list =productDetailService.findWarningOfProcurement(pg,null);
        productDetailList = list.getRecords();
        ExcelUtils.export(response,"采购预警信息",productDetailList, ProductDetailVO.class);
    }

}

